﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Chemix.Instrument
{
    public class Match : BaseBurnable
    {
        [Tooltip("Specifies the delay in seconds after which the match will be put out automatically.")]
        public float putOutDelay = 2f;

        public override bool OnFire
        {
            get
            {
                return onFire;
            }

            set
            {
                base.OnFire = value;

                if (value)
                {
                    StopCoroutine("WaitAndPutOutFire");
                    StartCoroutine("WaitAndPutOutFire");
                }
            }
        }
        
        private void Update()
        {
            var currentSpeed = (transform.position - m_LastPos).sqrMagnitude;
            if (currentSpeed > m_PutOutBySpeed)
            {
                OnFire = false;
            }
            m_LastPos = transform.position;
        }

        IEnumerator WaitAndPutOutFire()
        {
            yield return new WaitForSeconds(putOutDelay);
            OnFire = false;
            //Debug.Log("ChemixMatch: put off");
        }

        Vector3 m_LastPos;

        [Tooltip("If the speed of match exceeds this threshold, the match will be put out.")]
        [SerializeField]
        float m_PutOutBySpeed = 1000f;
    }
}